package com.knife.file.compress.zip;

import com.knife.file.compress.KnifeFileCompress;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties;
import org.springframework.util.ObjectUtils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * zip压缩实现
 *
 * @author： 76875
 * @date： 2022/3/30 星期三 15:25
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
public class KnifeFileZipCompress implements KnifeFileCompress {


    @Override
    public void compress(File srcFile, String zipFileName, boolean keepDirStructure) {
        if (ObjectUtils.isEmpty(srcFile) || !srcFile.exists()){
            return;
        }
        File targetFile = new File(zipFileName);
        ZipOutputStream zos = null;
        FileOutputStream fileOutputStream = null;
        try  {
            fileOutputStream = new FileOutputStream(targetFile);
            zos = new ZipOutputStream(fileOutputStream);
            compress(srcFile, zipFileName, zos, keepDirStructure);
        } catch (IOException e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (!ObjectUtils.isEmpty(zos)) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!ObjectUtils.isEmpty(fileOutputStream)){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @Override
    public void compress(File srcFile, String zipFileName, OutputStream outputStream, boolean keepDirStructure) {
        if (ObjectUtils.isEmpty(srcFile) || !srcFile.exists()){
            return;
        }
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(outputStream);
            compress(srcFile, zipFileName, zos, keepDirStructure);
        } catch (IOException e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (!ObjectUtils.isEmpty(zos)) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void compress(File file, String zipFileName, ZipOutputStream zos, boolean keepDirStructure) throws IOException {
        byte[] buffer = new byte[2048];
        if (file.isFile()) {
            // 单文件压缩，添加zip实体
            zos.putNextEntry(new ZipEntry(zipFileName));
            // 读取文件，并写入zip输出流
            try (FileInputStream fis = new FileInputStream(file);
                 BufferedInputStream bis = new BufferedInputStream(fis);) {
                int len;
                while ((len = bis.read(buffer)) != -1) {
                    zos.write(buffer, 0, len);
                }
                zos.closeEntry();
            }
        } else {
            File[] allFiles = file.listFiles();
            if (ObjectUtils.isEmpty(allFiles)) {
                // 空文件暂不处理

            } else {
                for (File currFile : allFiles) {
                    if (keepDirStructure) {
                        // 保存原来的文件结构，注意：file.getName()前面需要带上父文件夹的名字加一斜杠,
                        // 不然最后压缩包中就不能保留原来的文件结构,即：所有文件都跑到压缩包根目录下了
                        compress(currFile, zipFileName + "/" + currFile.getName(), zos, true);
                    } else {
                        compress(currFile, currFile.getName(), zos, false);
                    }
                }
            }
        }
    }
}
